<?php
/**
 * 用户算力矿机订单
 */
namespace app\common\model;
use think\Model;

class MemberArithmeticalPower extends Model
{
    protected $name = 'member_arithmetical_power';

    /**
     * 购买算力矿机详情
     * @param $id 算力ID
     * @return MemberLevel|array|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getDetails($id){
        $data = self::where('id',$id)->find();
        return $data;
    }

    /**
     * 统计矿机算力
     * @param int|array $mid   用户ID
     * @param string $maid 矿机ID
     * @param int $statistical_type 统计算力类型 1:矿机总算力 2:矿机剩余算力
     * @param int $pledge_type  1:非质押 2:质押 3:不限
     * @return float
     */
    public static function statistical_power($mid = '',$maid = '',$statistical_type=2,$pledge_type = 2){
        //已支付订单
        $data = self::where('pay_status',1);

        if($mid){   //查询用户
            if(is_array($mid)){ //判断是否是In查询
                $data = $data->whereIn('mid',$mid);
            }else{
                //某个用户
                $data = $data->where('mid',$mid);
            }
        }
        //查询指定某个算力矿机
        if(!empty($maid)) $data = $data->where('maid',$maid);

        //是否查询需要质押或不质押
        if($pledge_type != 3){
            if($pledge_type == 2) $data = $data->where('pledge',$pledge_type);
            if($pledge_type == 1){
                $data = $data->whereIn('pledge','1,2');
            }
        }

        //返回 矿机总算力
        if($statistical_type == 1){
            return $data->sum('arithmetical_power');
        }else{
            //返回 剩余矿机算力
            return $data->sum('surplus_arithmetical');
        }
    }

}